﻿using System;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using Microsoft.SharePoint;
using Microsoft.SharePoint.WebPartPages;
using System.IO;
using System.Collections;
using System.Linq;
namespace WebPartCollector.WebPartCollector
{
    public partial class WebPartCollectorUserControl : UserControl
    {
        public Boolean blnDocumentLib { get; set; }
        public Boolean blnList { get; set; }
        public Boolean blnQuickLaunch { get; set; }
        public Boolean blnMicrosoft { get; set; }
        public Boolean blnForm { get; set; }

        protected void Page_Load(object sender, EventArgs e)
        {
            CallWebParts();
        }

        private void CallWebParts()
        {            
            string webUrl = SPContext.Current.Web.Url;

            SPSite site = new SPSite(webUrl);
            SPWeb web = site.OpenWeb();
            SPListCollection listCollection;
            listCollection = web.Lists;

            //If only Document Libraries are to be shown
            if (blnDocumentLib)
            {
                listCollection = web.GetListsOfType(SPBaseType.DocumentLibrary);
            }
            //If Lists are only to be shown
            else if (blnList)
            {
                listCollection = web.GetListsOfType(SPBaseType.GenericList);
            }

            SPSecurity.RunWithElevatedPrivileges(delegate()
            {
                LiteralControl liControl = new LiteralControl();

                liControl.Text = "<div id='sidetree'><div class='treeheader'>&nbsp;</div><div id='sidetreecontrol'><a href='?#'>Collapse All</a> | <a href='?#'>" +
                                                   "Expand All</a></div></br><ul id='tree'>";

                foreach (SPList list in listCollection)
                {
                    //If Quick Launch items are to be ignored
                    if (blnQuickLaunch && !list.OnQuickLaunch)
                    {
                        continue;
                    }

                    SPFolder rootFolder = list.RootFolder;

                    // If there are subfolders called Forms, then it enters here
                    if (rootFolder.SubFolders.Count > 0)
                    {
                        Boolean blnCount = false;
                        Boolean blnWebpart = true;
                        Boolean blnNoCount = true;

                        string strWebPart = "None";

                        string strLiteralText = "<li><strong>" + list.Title + "</strong><ul>";

                        for (int i = 0; i < rootFolder.SubFolders.Count; i++)
                        {
                            if (rootFolder.SubFolders[i].Name == "Forms" && blnForm)
                            {
                                continue;
                            }
                            for (int j = 0; j < rootFolder.SubFolders[i].Files.Count; j++)
                            {
                                if (rootFolder.SubFolders[i].Files[j].ToString().Contains(".aspx"))
                                {
                                    // blnForm checks whether this needs to be ignored.
                                    if ((rootFolder.SubFolders[i].Files[j].ToString().Contains("DispForm.aspx") ||
                                        rootFolder.SubFolders[i].Files[j].ToString().Contains("NewForm.aspx") ||
                                        rootFolder.SubFolders[i].Files[j].ToString().Contains("EditForm.aspx") ||
                                        rootFolder.SubFolders[i].Files[j].ToString().Contains("AllItems.aspx")) &&
                                        blnForm)
                                    {
                                        continue;
                                    }

                                    blnCount = true;

                                    SPLimitedWebPartManager manager = web.GetLimitedWebPartManager((webUrl + "/" + rootFolder.SubFolders[i].Files[j]).ToString(), System.Web.UI.WebControls.WebParts.PersonalizationScope.Shared);
                                    SPLimitedWebPartCollection webPartColl = manager.WebParts;

                                    System.Web.UI.WebControls.WebParts.WebPart webp = webPartColl[0];

                                    string strWebpartText = "<li>" + rootFolder.SubFolders[i].Files[j].ToString() + "<ul>";


                                    //blnMicrosoft is used to check Microsoft OOTB web parts

                                    for (int k = 0; k < webPartColl.Count; k++)
                                    {
                                        if (webPartColl[k].GetType().ToString().Contains("Microsoft.SharePoint") && blnMicrosoft)
                                        {
                                            blnWebpart = false;
                                        }
                                        else
                                        {
                                            blnWebpart = true;
                                            strWebPart = "Present";
                                            blnNoCount = true;
                                        }

                                        strWebpartText += "<li>" + webPartColl[k].DisplayTitle + "--" + webPartColl[k].GetType() + "</li>";
                                    }
                                    strWebpartText += "</ul></li>";

                                    if (blnWebpart)
                                    {
                                        strLiteralText += strWebpartText;
                                    }
                                    else if (blnMicrosoft && strWebPart != "Present")
                                    {
                                        blnNoCount = false;
                                    }
                                }
                            }
                        }
                        strLiteralText += "</ul></li>";

                        if (blnCount && blnNoCount)
                        {
                            liControl.Text += strLiteralText;
                        }
                    }

                    if (rootFolder.Files.Count > 0)
                    {
                        Boolean blnCount = false;
                        Boolean blnWebpart = true;
                        Boolean blnNoCount = true;

                        string strWebPart = "None";

                        string strLiteralText = "<li><strong>" + list.Title + "</strong><ul>";

                        for (int a = 0; a < rootFolder.Files.Count; a++)
                        {
                            if (rootFolder.Files[a].ToString().Contains(".aspx"))
                            {
                                if ((rootFolder.Files[a].Url.Contains("DispForm.aspx") ||
                                        rootFolder.Files[a].Url.Contains("NewForm.aspx") ||
                                        rootFolder.Files[a].Url.Contains("EditForm.aspx") ||
                                        rootFolder.Files[a].Url.Contains("AllItems.aspx")) &&
                                        blnForm)
                                {
                                    continue;
                                }

                                blnCount = true;

                                SPLimitedWebPartManager manager = web.GetLimitedWebPartManager((webUrl + "/" + rootFolder.Files[a]).ToString(), System.Web.UI.WebControls.WebParts.PersonalizationScope.Shared);

                                SPLimitedWebPartCollection webPartColl = manager.WebParts;

                                string strWebpartText = "<li>" + rootFolder.Files[a].ToString() + "<ul>";

                                for (int k = 0; k < webPartColl.Count; k++)
                                {
                                    if (webPartColl[k].GetType().ToString().Contains("Microsoft.SharePoint") && blnMicrosoft)
                                    {
                                        blnWebpart = false;
                                    }
                                    else
                                    {
                                        blnWebpart = true;
                                        strWebPart = "Present";
                                        blnNoCount = true;
                                    }

                                    strWebpartText += "<li>" + webPartColl[k].DisplayTitle + "--" + webPartColl[k].GetType() + "</li>";
                                }
                                strWebpartText += "</ul></li>";

                                if (blnWebpart)
                                {
                                    strLiteralText += strWebpartText;
                                }
                                else if (blnMicrosoft && strWebPart != "Present")
                                {
                                    blnNoCount = false;
                                }
                            }
                        }
                        strLiteralText += "</ul></li>";

                        if (blnCount && blnNoCount)
                        {
                            liControl.Text += strLiteralText;
                        }
                    }
                }
                this.Controls.Add(liControl);
            });
        }
    }
}
